import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    num: 0// 这个相当于方法里的变量 但是要引用

  },
  mutations: { //相当于全局方法 但是不能直接用 要用$store.commit('方法名'[,变量])
    add_num (state, n) {
      state.num += n
      localStorage.setItem('555', state.num)
    },
    getlocal (state) {
      if (localStorage.getItem('555')) {
        state.num = JSON.parse(localStorage.getItem('555'))
        state.add_num()
      } else {
        state.num = 0
      }
    },
    pdd_num(state,n){
      state.num=state.num-n
      localStorage.setItem('555', state.num)
    }
  },

  actions: { // 相当于$store.commit('mutations里的方法名'[,变量])
    setValue (state, n) {
      state.commit('add_num', n)
    },
    get_local(state,n){
      state.commit('getlocal')
    },
    set_num(state,n){
      state.commit('pdd_num',n)
    }
  },
  getters:{

  },
  modules: {

  }
})
